
import type {ActionType, ProColumns} from '@ant-design/pro-components';
import {PageContainer, ProTable, TableDropdown} from '@ant-design/pro-components';
import {Button, Divider, Dropdown, Input, Modal, Popconfirm, Space, Typography} from 'antd';
import {
  deleteUser, getAssetCategoryList, getAssetLabel,
  getAssetList,
  getOrgList,
  getOrgListByFatherOrg,
  listUsers, searchAssetByCategory, searchAssetByLabel, searchAssetByName,
  searchOrgByName
} from "@/services/ant-design-pro/api";
import message from "antd/es/message";
import {useRef, useState} from "react";
import UpdateModal from "@/pages/Admin/AssetLabelMange/components/UpdateModal";
import CreateModal from "@/pages/Admin/AssetLabelMange/components/CreateModal";
import 'antd/dist/antd.css';



const valueEnum = {
  0: 'close',
  1: 'running',
  2: 'online',
  3: 'error',
};

/**
 *  删除节点
 * @param selectedRows
 */
const doDelete = async (selectedRows: API.CurrentUser[]) => {
  const hide = message.loading('正在删除');
  if (!selectedRows) return true;
  try {
    const result = await deleteUser({
      id: selectedRows.find((row) => row.id)?.id || 0,
    });
    if(result.code === 200){
      message.success('操作成功');
    }else {
      message.error(result.description);
    }
  } catch (e: any) {
    message.error('操作失败，' + e.message);
  } finally {
    hide();
  }
};


/**
 * 用户管理页面
 * @constructor
 */
const AdminAssetPage: React.FC<unknown> = () => {

  const [createModalVisible, setCreateModalVisible] = useState<boolean>(false);
  const [updateModalVisible, setUpdateModalVisible] = useState<boolean>(false);
  const [updateData, setUpdateData] = useState<API.AssetLabel>({});
  const actionRef = useRef<ActionType>();

  const columns: ProColumns<API.AssetLabel>[] = [
    {
      title: '序号',
      dataIndex: 'index',
      valueType: 'indexBorder',
      width: 48,
    },
    {
      title: '资产标签',
      dataIndex: 'assetlabel',
      copyable: true
    },



    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (_, record) => (
        <Space split={<Divider type="vertical" />}>
          <Typography.Link
            onClick={() => {
              setUpdateData(record);
              setUpdateModalVisible(true);
            }}
          >
            修改
          </Typography.Link>
          <Popconfirm
            title="您确定要删除么？"
            onConfirm={() => doDelete([record])}
            okText="确认"
            cancelText="取消"
          >
            <Typography.Link type="danger">删除</Typography.Link>
          </Popconfirm>
        </Space>
      ),
    },

  ];

  return (
    <PageContainer>
      <ProTable<API.Asset>
        headerTitle="资产标签管理"
        actionRef={actionRef}
        columns={columns}
        // @ts-ignore
        request={async (params, sorter, filter) => {
          // 表单搜索项会从 params 传入，传递给后端接口。
          console.log(params)
          if (params.assetlabel) {
            // 如果有 assetname 参数，调用按名称查询接口
            const result = await getAssetLabel(params.assetlabel);
            return Promise.resolve({
              data: result.data
            });
          }
          else {
            // 否则调用查询所有组织机构的接口
            const result = await getAssetLabel();
            return Promise.resolve({
              data: result.data
            });
          }
        }}
        rowKey="key"
        pagination={{
          showQuickJumper: true,
        }}
        search={{
          layout: 'vertical',
          defaultCollapsed: false,
        }}
        dateFormatter="string"
        toolbar={{
          title: '资产标签列表',
          tooltip: '',
        }}
        toolBarRender={() => [
          <Button
            key="1"
            type="primary"
            onClick={() => setCreateModalVisible(true)}
          >
            添加资产标签
          </Button>,
        ]}
      />
      <CreateModal
        modalVisible={createModalVisible}
        columns={columns}
        onSubmit={() => setCreateModalVisible(false)}
        onCancel={() => setCreateModalVisible(false)}
      />
      <UpdateModal
        oldData={updateData}
        modalVisible={updateModalVisible}
        columns={columns}
        onSubmit={() => setUpdateModalVisible(false)}
        onCancel={() => setUpdateModalVisible(false)}
      />

    </PageContainer>
  );
};
export default AdminAssetPage;
